﻿@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>用户权限</title>
    <link href="~/Content/Easy_UI/css/easyui.css" rel="stylesheet" />
    <link href="~/Content/Easy_UI/css/icon.css" rel="stylesheet" />
    <link href="~/Content/Easy_UI/css/menu.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-1.10.2.min.js"></script>
    <script src="~/Content/Easy_UI/js/datapattern.js"></script>
    <script src="~/Content/Easy_UI/js/easyui-lang-zh_CN.js"></script>
    <script src="~/Content/Easy_UI/js/jquery.easyui.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script type="text/javascript">
        $(function () {
            //隐藏添加,修改From表单
            $("#addDiv").css("display", "none");
            $("#editDiv").css("display", "none");

            loadData();
            checkboxOnClick();
        });

        //图书类型的复选框
        function checkboxOnClick() {
            $("input[type=checkbox]").click(function () {
                var a = [];
                var selectId = "";
                var checkbox_value = $(this).attr('value');
                var ck = $(':input[type=checkbox]');
                ck.each(function () {
                    if ($(this).is(':checked')) {
                        if ($(this).val() != '')
                            a.push($(this).val());
                    }
                })
                for (var j = 0; j < a.length; j++) {
                    selectId = selectId + a[j] + ','
                }
                selectId = selectId.substr(0, selectId.length - 1);
                $('#txtBookType').val(selectId);
            });
        }

        //select的change事件
        function binActionUserTypeChange() {
            var type = $('#selectId').find('option:selected').text();
            loadData(type);
        }

        //加载数据
        function loadData(pars) {
            $('#tt').datagrid({
                url: '/UserAction/ShowAllUserAction',
                title: '用户数据',
                width: 800,
                height: 505,
                fitColumns: true, //列自适应
                nowrap: false,
                idField: 'Id',//主键列的列明
                loadMsg: '正在加载用户借阅权限的信息...',
                pagination: true,//是否有分页
                singleSelect: false,//是否单行选择
                pageSize: 15,//页大小，一页多少条数据
                pageNumber: 1,//当前页，默认的
                pageList: [10, 15, 20],
                //queryParams: pars,//往后台传递参数
                queryParams: {
                    userType: pars
                },//往后台传递参数
                columns: [[//c.UserName, c.UserPass, c.Email, c.RegTime
                    { field: 'ck', checkbox: true, align: 'left', width: 50 },
                    { field: 'UserType', title: '读者类型', width: 120 },
                    {
                        field: 'DepartmentName', title: '馆藏单位', width: 120,
                        formatter: function (value, row, index) {
                            return value ? "安徽农业大学" : "未知学校";
                        }
                    },
                    {
                        field: 'CollectionName', title: '典藏地', width: 120,
                        formatter: function (value, row, index) {
                            if (value == "0008")
                                return '社科书库';
                            else if (value == '0009')
                                return '工具书库';
                            else if (value == '0010')
                                return '自然书库';
                            else if (value == '0013')
                                return '文艺书库';
                            else if (value == '0024')
                                return '教材书库';
                            else if (value == '0025')
                                return '古籍书库';
                            else if (value == '0026')
                                return '老图书馆书库';
                        }
                    },
                    {
                        field: 'BookType', title: '单册类型', width: 120,
                        formatter: function (value, row, index) {
                            var value = value.split(',');
                            var type = '';
                            for (var i = 0; i < value.length; i++) {
                                if (value[i] == '0003') {
                                    type = type + "小说" + ',';
                                }
                                else if (value[i] == '0001') {
                                    type = type + "普通" + ',';
                                }
                            }
                            return type.substr(0, type.length - 1);
                        }
                    },
                    { field: 'UserDepartmentName', title: '读者办证单位', width: 120 }
                ]],
                toolbar: [{
                    id: 'btnDelete',
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        deleteInfo();
                    }
                }, {
                    id: 'btnAdd',
                    text: '添加',
                    iconCls: 'icon-add',
                    handler: function () {
                        addInfo();
                    }
                }, {
                    id: 'btnEidt',
                    text: '编辑',
                    iconCls: 'icon-edit',
                    handler: function () {
                        showEditInfo();//展示要修改的用户数据
                    }
                }],
            });
        }

        //删除数据
        function deleteInfo() {
            var rows = $('#tt').datagrid('getSelections');//获取所选择的行
            if (!rows || rows.length == 0) {
                //alert("请选择要删除的商品！");
                $.messager.alert("提醒", "请选择要删除的记录!", "error");
                return;
            }

            $.messager.confirm("提示", "确定要删除数据吗？", function (r) {
                if (r) {
                    //要删除数据，获取要删除的记录的Id
                    var strUserType = "";
                    var strCollectionCode = ""
                    for (var i = 0; i < rows.length; i++) {
                        strUserType = strUserType + rows[i].UserType + ",";
                        strCollectionCode = strCollectionCode + rows[i].CollectionName + ",";
                    }
                    //去掉最后一个逗号“，”
                    strUserType = strUserType.substr(0, strUserType.length - 1);
                    strCollectionCode = strCollectionCode.substr(0, strCollectionCode.length - 1);

                    //将获取的要删除的记录的Id值发送到服务端
                    $.post("/UserAction/Delete", { "usertype": strUserType, "collectioncode": strCollectionCode }, function (data) {
                        if (data == "ok") {
                            //"reload"方法加载表格不会跳转到第一页
                            $('#tt').datagrid("reload");

                            //清除上次操作的历史的记录 异步请求会把上次操作的id继续带上
                            $('#tt').datagrid('clearSelections');
                        }
                        else {
                            $.messager.alert("提醒", "删除失败！", "error")
                        }
                    });
                }
            });
        }

        //添加数据
        function addInfo() {

            $("#addDiv").css("display", "block");

            //添加用户的操作是一个异步操作 清空文本框
            $("addForm input").val("");
            //数据传递
            var type = $('#selectId').find('option:selected').text();
            $('#textUserType').val(type);

            $("#addDiv").dialog({
                collapsible: true,//可折叠
                maximizable: true,//可最大化
                minimizable: true,//可最小化
                resizable: true,//可缩放
                modal: true,//制造一种模态
                buttons: [{
                    text: "确定",
                    iconCls: "icon-ok",
                    handler: function (data) {
                        var b = [];
                        var ck = $(':input[type=checkbox]');
                        ck.each(function () {
                            if ($(this).is(':checked')) {
                                b.push($(this).val());
                            }
                        })
                        var checkselectId = "";
                        for (var i = 0; i < b.length; i++) {
                            if (b[i] != '')
                                checkselectId = checkselectId + b[i] + ','
                        }
                        checkselectId = checkselectId.substr(0, checkselectId.length - 1);
                        $("#textBookType").val(checkselectId);

                        //在表单提交之前要校验数据的合理性
                        validateInfo($("#addForm"));

                        $("#addForm").submit();//提交表单
                    }
                }, {
                    text: '取消',
                    handler: function () {
                        $("#addDiv").dialog("close");
                    }
                }]
            });



        }

        //完成添加后调用该方法
        function afterAdd(data) {
            if (data == "ok") {
                $("#addDiv").dialog("close");
                $.messager.alert("提示", "添加成功");
                //"reload"方法加载表格不会跳转到第一页
                $('#tt').datagrid("reload");

                //添加用户的操作是一个异步操作 清空文本框
                $("addForm input").val("");
            }
            else {
                $.messager.alert("提示", "添加失败");
            }
        }

        //表单校验
        function validateInfo(control) {
            control.validate({//表示对哪个form表单进行校验，获取form标签的id属性的值
                rules: {//表示验证规则
                    TSLXDM: "required",//表示对哪个表单元素进行校验，要写具体的表单元素的name属性的值
                    ZDJYCS: "required",
                    JYTS: "required",
                    XJCS: "required",
                    XJTS: "required",
                    YYCS: "required",
                    YYTS: "required",
                    BLCS: "required",
                    GQFJ: "required"
                },
                messages: {
                    TSLXDM: "至少选择一种图书类型",
                    ZDJYCS: "请输入可借册数",
                    JYTS: "请输入借期天数",
                    XJCS: "请输入续借次数",
                    XJTS: "请输入续借天数",
                    YYCS: "请输入预约册数",
                    YYTS: "请输入预约天数",
                    BLCS: "请输入预约保留册数",
                    GQFJ: "请输入过期罚金"
                }
            });
        }

        //展示要修改的用户数据
        function showEditInfo() {
            //在弹窗之前要拿到要修改的用户的数据
            //判断一下用户是否选择了要修改的数据
            var rows = $("#tt").datagrid("getSelections");//获取所选择的行
            if (rows.length != 1) {
                $.messager.alert("提示", "请选择要修改的数据", "error");
                return;//跳出此方法
            }

            //将要修改的数据查询出来，显示到文本框中
            var userType = rows[0].UserType;//
            var collectionName = rows[0].CollectionName;//
            var b = [];
            $("#txtUserType").val('');
            $("#txtCollectionName").val('');
            $("#txtUserDepartmentName").val('');
            $("#txtCollectionName").val('');
            $("#txtBookType0001").prop('checked', false);
            $("#txtBookType0003").prop('checked', false);
            $("#txtCollectionDepartmentName").val('');
            $("#txtBorrowNum").val('');
            $("#txtBorrowDay").val('');
            $("#txtRewBorrowNum").val('');
            $("#txtRewBorrowDay").val('');
            $("#txtHoldNum").val('');
            $("#txtHoldDay").val('');
            $("#txtKeepNum").val('');
            $("#txtOverFine").val('');

            $.get("/UserAction/EditUserAction", { "userType": userType, "collectionName": collectionName }, function (data) {
                //这里其实返回回来的是一个json对象,进行数据绑定
                $("#txtUserType").val(data.DZLX);
                $("#txtCollectionName").find("option[value = '" + data.DCDDM + "']").attr("selected", "selected");
                $("#txtUserDepartmentName").val(data.DZDWMC);
                $("#txtCollectionName").val(data.DCDDM);
                var value = data.TSLXDM.split(',');
                for (var i = 0; i < value.length; i++) {
                    if (value[i] == '0001')
                        $("#txtBookType0001").prop('checked', true);
                    else if (value[i] == '0003')
                        $("#txtBookType0003").prop('checked', true);
                }
                var ck = $(':input[type=checkbox]');
                ck.each(function () {
                    if ($(this).is(':checked')) {
                        b.push($(this).val());
                    }
                })
                var checkselectId = "";
                for (var i = 0; i < b.length; i++) {
                    if (b[i] != '')
                        checkselectId = checkselectId + b[i] + ','
                }
                checkselectId = checkselectId.substr(0, checkselectId.length - 1);
                $("#txtBookType").val(checkselectId);
                $("#CollectionDepartmentName").val(data.GCDWDM);
                $("#txtCollectionDepartmentName").val(data.GCDWDM == "1" ? "安徽农业大学" : "未知学校");
                $("#txtBorrowNum").val(data.ZDJYCS);
                $("#txtBorrowDay").val(data.JYTS);
                $("#txtRewBorrowNum").val(data.XJCS);
                $("#txtRewBorrowDay").val(data.XJTS);
                $("#txtHoldNum").val(data.YYCS);
                $("#txtHoldDay").val(data.YYTS);
                $("#txtKeepNum").val(data.BLCS);
                $("#txtOverFine").val(data.GQFJ);
            });

            $("#editDiv").css("display", "block");
            $("#editDiv").dialog({
                collapsible: true,//可折叠
                maximizable: true,//可最大化
                minimizable: true,//可最小化
                resizable: true,//可缩放
                modal: true,//制造一种模态
                buttons: [{
                    text: "ok",
                    iconCls: "icon-ok",
                    handler: function (data) {
                        //在表单提交之前要校验数据的合理性
                        validateInfo($("#editForm"));
        
                        $("#txtCollectionName").removeAttr("disabled");
                        $("#editForm").submit();//提交表单
                    }
                }, {
                    text: 'Cancel',
                    handler: function () {
                        $("#editDiv").dialog("close");
                    }
                }]
            });
        }

        //更新以后调用的方法
        function afterEdit(data) {
            if (data == "ok") {
                $("#editDiv").dialog("close");
                $.messager.alert("提示", "更新成功");
                //"reload"方法加载表格不会跳转到第一页
                $('#tt').datagrid("reload");
            }
            else {
                $.messager.alert("提示", "更新失败");
            }
        }
    </script>
</head>
<body>
    <div>
        <hr />
        <select id="selectId" onchange="binActionUserTypeChange()">
            <option value="0">本科生</option>
            <option value="1">博士生</option>
            <option value="2">教师</option>
            <option value="3">学生</option>
            <option value="4">研究生</option>
            <option value="5">专升本</option>
        </select>
        <hr />
        <table id="tt" style="width:600px;" title="标题" iconcls="icon-edit"></table>
    </div>
    <!-----------------------添加数据------------------------------>
    <div id="addDiv" style="width:600px;height:350px;" title="添加用户数据">
        @using (Ajax.BeginForm("AddUserAction", "UserAction", new { }, new AjaxOptions { HttpMethod = "post", OnSuccess = "afterAdd" }, new { id = "addForm" }))
        {
        <table>
            <tr>
                <th>读&nbsp;者&nbsp;类&nbsp;型:</th>
                <td><input type="text" readonly="readonly" id="textUserType" name="DZLX" /></td>
                <th>典&nbsp;藏&nbsp;地:</th>
                <td>
                    <select name="DCDDM">
                        <option value="0008">社科书库</option>
                        <option value="0009">工具书库</option>
                        <option value="0010">自然书库</option>
                        <option value="0013">文艺书库</option>
                        <option value="0024">教材书库</option>
                        <option value="0025">古籍书库</option>
                        <option value="0026">老图书馆书库</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th>单&nbsp;册&nbsp;类&nbsp;型:</th>
                <td>
                    <input type="checkbox" name="bookType" value="0001">普通<br />
                    <input type="checkbox" name="bookType" value="0003">小说<br />
                    <input type="hidden" name="TSLXDM" id="textBookType" />
                </td>
                <th>馆&nbsp;藏&nbsp;单&nbsp;位:</th>
                <td>
                    <select name="GCDWDM">
                        <option value="1" selected="selected">安徽农业大学</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th>可&nbsp;借&nbsp;数&nbsp;量:</th>
                <td>
                    <input type="text" name="ZDJYCS" />
                </td>
                <th>借&nbsp;期:</th>
                <td><input type="text" name="JYTS" /></td>
            </tr>
            <tr>
                <th>续&nbsp;借&nbsp;次&nbsp;数:</th>
                <td>
                    <input type="text" name="XJCS" />
                </td>
                <th>续&nbsp;借&nbsp;天&nbsp;数:</th>
                <td><input type="text" name="XJTS" /></td>
            </tr>
            <tr>
                <th>预&nbsp;约&nbsp;册&nbsp;数:</th>
                <td>
                    <input type="text" name="YYCS" />
                </td>
                <th>预&nbsp;约&nbsp;天&nbsp;数:</th>
                <td><input type="text" name="YYTS" /></td>
            </tr>
            <tr>
                <th>保&nbsp;留&nbsp;册&nbsp;数:</th>
                <td>
                    <input type="text" name="BLCS" />
                </td>
                <th>过&nbsp;期&nbsp;罚&nbsp;金:</th>
                <td><input type="text" name="GQFJ" /></td>
            </tr>
        </table>
        }
    </div>
    <!-----------------------修改数据------------------------------>
    <div id="editDiv" style="width:600px;height:350px;" title="修改用户流通权限数据">
        @using (Ajax.BeginForm("EditUserAction", "UserAction", new { }, new AjaxOptions { HttpMethod = "post", OnSuccess = "afterEdit" }, new { id = "editForm" }))
        {
            <input type="hidden" name="DZLX" id="txtUserType" />
            <input type="hidden" name="GCDWDM" id="CollectionDepartmentName" />
            <table>
                <tr>
                    <th>读&nbsp;者&nbsp;办&nbsp;证&nbsp;单&nbsp;位:</th>
                    <td><input type="text" readonly="readonly" name="DZDWMC" id="txtUserDepartmentName" /></td>
                    <th>典&nbsp;藏&nbsp;地:</th>
                    <td>
                        <select name="DCDDM" disabled="disabled" id="txtCollectionName">
                            <option value="0008">社科书库</option>
                            <option value="0009">工具书库</option>
                            <option value="0010">自然书库</option>
                            <option value="0013">文艺书库</option>
                            <option value="0024">教材书库</option>
                            <option value="0025">古籍书库</option>
                            <option value="0026">老图书馆书库</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>单&nbsp;册&nbsp;类&nbsp;型:</th>
                    <td>
                        <input type="checkbox" id="txtBookType0001" name="bookType" value="0001">普通<br />
                        <input type="checkbox" id="txtBookType0003" name="bookType" value="0003">小说<br />
                        <input type="hidden" name="TSLXDM" id="txtBookType" />
                    </td>
                    <th>馆&nbsp;藏&nbsp;单&nbsp;位:</th>
                    <td>
                        <input type="text" readonly="readonly" id="txtCollectionDepartmentName" />
                    </td>
                </tr>
                <tr>
                    <th>可&nbsp;借&nbsp;数&nbsp;量:</th>
                    <td>
                        <input type="text" name="ZDJYCS" id="txtBorrowNum" />
                    </td>
                    <th>借&nbsp;期:</th>
                    <td><input type="text" name="JYTS" id="txtBorrowDay" /></td>
                </tr>
                <tr>
                    <th>续&nbsp;借&nbsp;次&nbsp;数:</th>
                    <td>
                        <input type="text" name="XJCS" id="txtRewBorrowNum" />
                    </td>
                    <th>续&nbsp;借&nbsp;天&nbsp;数:</th>
                    <td><input type="text" name="XJTS" id="txtRewBorrowDay" /></td>
                </tr>
                <tr>
                    <th>预&nbsp;约&nbsp;册&nbsp;数:</th>
                    <td>
                        <input type="text" name="YYCS" id="txtHoldNum" />
                    </td>
                    <th>预&nbsp;约&nbsp;天&nbsp;数:</th>
                    <td><input type="text" name="YYTS" id="txtHoldDay" /></td>
                </tr>
                <tr>
                    <th>保&nbsp;留&nbsp;册&nbsp;数:</th>
                    <td>
                        <input type="text" name="BLCS" id="txtKeepNum" />
                    </td>
                    <th>过&nbsp;期&nbsp;罚&nbsp;金:</th>
                    <td><input type="text" name="GQFJ" id="txtOverFine" /></td>
                </tr>
            </table>
        }
    </div>
</body>
</html>
